package com.ybkj.o2o.zhuanxian.controller.res;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.zhuanxian.common.error.ErrorConstantV4;
import com.ybkj.o2o.zhuanxian.common.error.ResultTransport;
import com.ybkj.o2o.zhuanxian.entity.Order;
import com.ybkj.o2o.zhuanxian.entity.OrderMemo;
import com.ybkj.o2o.zhuanxian.manager.OrderManager;
import com.ybkj.o2o.zhuanxian.manager.OrderMemoManager;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("api/orderMemo")
public class OrderMemoController {

  @Autowired
  private OrderMemoManager orderMemoManager;
  @Autowired
  private OrderManager orderManager;

  /**
   * 创建订单备注 .
   * 
   * @param orderId     订单Id
   * @param memo        备注
   * @param operator    操作人:管理员,客户
   * @param appKey      区别系统
   */
  @RequestMapping(value = "remark", method = RequestMethod.POST)
  @ResponseBody
  public ResultTransport create(
      @RequestParam("orderId") Long orderId,
      @RequestParam("memo") String memo, 
      @RequestParam("operator") String operator,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(memo), "备注不能为空");
    Order order = orderManager.findByIdAndAppKey(orderId, appKey);
    Preconditions.checkArgument(order != null, "订单不存在");
    try {
      OrderMemo orderMemo = new OrderMemo();
      orderMemo.setMemo(memo);
      orderMemo.setOperator(operator);
      orderMemo.setOrderId(order.getId());
      orderMemo.setOrderNo(order.getOrderNo());
      orderMemo.setCreated(new Date());
      orderMemo.setUpdated(new Date());
      OrderMemo om = orderMemoManager.insert(orderMemo);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(om);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }
  
  /**
   * 根据订单id和appkey查询订单备注 .
   * @param orderId     订单id
   * @param appKey      区分系统
   * @return ResultTransport
   */
  @RequestMapping(value = "findMemo", method = RequestMethod.GET)
  @ResponseBody
  public ResultTransport findMemos(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(orderId != null && orderId != 0, "订单Id不能为空");
    Order order = orderManager.findByIdAndAppKey(orderId, appKey);
    Preconditions.checkArgument(order != null, "此订单不存在");
    try {

      List<OrderMemo> memos = orderMemoManager.findByOrderId(orderId);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(memos);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

}
